/*
 * File:   10067.cpp
 * Author: xerxes
 *
 * Created on September 17, 2011, 8:30 AM
 *
 * I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

inline void wait(double seconds) {
    double endtime = clock()+(seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}
string source, sink;
map< string, int > forbid;

inline void Read() {
    source = sink = "";
    int x;

    fo(i, 0, 4) {
        CI(x);
        source += (x + '0');
    }

    fo(i, 0, 4) {
        CI(x);
        sink += (x + '0');
    }
    //cout<<source<<" "<<sink<<"\n";
    int n;
    string ss;
    CI(n);
    forbid.clear();

    fo(i, 0, n) {
        ss = "";

        fo(j, 0, 4) {
            CI(x);
            ss += (x + '0');
        }
        //cout<<"FORBID : "<<ss<<"\n";
        forbid[ss] = 1;
    }
}

inline int rotate(char nw, int sign) {
    int var = nw - '0';
    //sign==1 //right
    //sign==0 //left
    if (sign == 0) {
        var = (var == 9 ? 0 : var + 1);
    } else {
        var = (var == 0 ? 9 : var - 1);
    }
    return var + '0';
}

inline void Proc(string &source, string &sink) {
    queue< string > q;
    map< string, int > vis;

    if (forbid.count(source) || forbid.count(sink)) {
        goto L;
    }

    vis[source] = 0;
    q.push(source);
    while (q.empty() == false) {
        string father = q.front();
        int move = vis[father];

        //cout<<"Father : Move "<<father<<" "<<move<<"\n";wait(2);
        q.pop();
        if (father == sink) {
            cout << move << "\n";
            return;
        }

        fo(i, 0, 4) {

            fo(j, 0, 2) {
                char nw = rotate(father[i], j);
                swap(father[i], nw);

                if (!vis.count(father) && !forbid.count(father)) {
                    vis[father] = move + 1;
                    q.push(father);
                }

                swap(father[i], nw);
            }
        }

    }
L:
    cout << -1 << "\n";
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
#endif
    int cases = 1;
    for (CI(cases); cases; --cases) {
        Read();
        Proc(source, sink);
    }

    return 0;
}


